package com.bus.core.dao.impl;

import java.util.List;

import javax.persistence.Query;

import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Repository;

import com.bus.common.hibernate4.Updater;
import com.bus.common.jpa.JpaBaseDao;
import com.bus.common.page.Pagination;
import com.bus.core.dao.UnifiedUserDao;
import com.bus.core.entity.UnifiedUser;

@CacheConfig(cacheNames="UnifiedUser")
@Repository
public class UnifiedUserDaoImpl extends JpaBaseDao<UnifiedUser, Integer>
		implements UnifiedUserDao {

	public int countByEmail(String email) {
		String hql = "select count(*) from UnifiedUser bean where bean.email=:email";
		Query query = getEntityManager().createQuery(hql);
		query.setParameter("email", email);
		return ((Number) query.getSingleResult()).intValue();
	}

	public UnifiedUser findById(Integer id) {
		UnifiedUser entity = get(id);
		return entity;
	}

	@CacheEvict
	public UnifiedUser save(UnifiedUser bean) {
		getEntityManager().persist(bean);
		return bean;
	}

	@CacheEvict
	public UnifiedUser deleteById(Integer id) {
		UnifiedUser entity = super.get(id);
		if (entity != null) {
			getEntityManager().remove(entity);
		}
		return entity;
	}

	@Override
	protected Class<UnifiedUser> getEntityClass() {
		return UnifiedUser.class;
	}

	@Override
	public UnifiedUser getByUsername(String username) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<UnifiedUser> getByEmail(String email) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Pagination getPage(int pageNo, int pageSize) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public UnifiedUser updateByUpdater(Updater<UnifiedUser> updater) {
		// TODO Auto-generated method stub
		return null;
	}
}